﻿namespace HutongGames.PlayMakerEditor
{
    using System;
    using System.Runtime.InteropServices;
    using UnityEditor;
    using UnityEngine;

    public class BugReportWindow : EditorWindow
    {
        private ScoutArea area;
        private Vector2 controlsScrollPosition;
        private string description;
        private string email;
        private string errorString;
        private string extra;
        private readonly string[] frequencyChoices = new string[] { "Always", "Sometimes, but not always", "This is the first time" };
        private int frequencyIndex;
        private bool isValid;
        private WWW www;

        private string BuildReportBody([Optional, DefaultParameterValue(false)] bool offline)
        {
            string str = "";
            string newLine = Environment.NewLine;
            if (offline)
            {
                str = str + this.description + newLine;
            }
            string str3 = str;
            str = (str3 + newLine + "Frequency: " + this.frequencyChoices[this.frequencyIndex] + newLine) + newLine + this.extra + newLine;
            if (offline)
            {
                str = str + this.email + newLine;
            }
            object obj2 = ((str + newLine + "Unity Info:" + newLine) + "Unity Version: " + Application.unityVersion + newLine) + "Playmaker Version: " + FsmEditorUtility.GetPlayMakerVersionString() + newLine;
            object obj3 = ((string.Concat(new object[] { obj2, "BuildTarget: ", EditorUserBuildSettings.activeBuildTarget, newLine }) + newLine + "System Info:" + newLine) + "OS: " + SystemInfo.operatingSystem + newLine) + "Processor: " + SystemInfo.processorType + newLine;
            return (string.Concat(new object[] { obj3, "System Memory: ", SystemInfo.systemMemorySize, newLine }) + "Graphics Device: " + SystemInfo.graphicsDeviceName + newLine);
        }

        private void CopyReportToClipboard()
        {
            EditorGUIUtility.systemCopyBuffer = this.BuildReportBody(true);
        }

        private bool IsValidSetup()
        {
            this.errorString = "";
            if (string.IsNullOrEmpty(this.description))
            {
                this.errorString = this.errorString + "Please Enter a Title\n";
            }
            if (string.IsNullOrEmpty(this.extra))
            {
                this.errorString = this.errorString + "Please Enter a Description\n";
            }
            return (this.errorString == "");
        }

        private void OnEnable()
        {
            base.title = "PlayMaker";
            base.wantsMouseMove = true;
            base.autoRepaintOnSceneChange = true;
            if (base.position.height < 200f)
            {
                base.position = new Rect(base.position.x, base.position.y, base.position.width, 600f);
            }
            base.minSize = new Vector2(500f, 200f);
            this.Reset();
        }

        private void OnFocus()
        {
            this.UpdateGUI();
        }

        private void OnGUI()
        {
            if (!FsmEditorStyles.IsInitialized())
            {
                FsmEditorStyles.Init();
            }
            FsmEditorGUILayout.ToolWindowLargeTitle(this, "Submit Bug Report");
            EditorGUIUtility.LookLikeControls(200f);
            this.controlsScrollPosition = EditorGUILayout.BeginScrollView(this.controlsScrollPosition, new GUILayoutOption[0]);
            GUILayout.Label("Title", EditorStyles.boldLabel, new GUILayoutOption[0]);
            this.description = GUILayout.TextField(this.description, new GUILayoutOption[0]);
            GUILayout.Label("Description", EditorStyles.boldLabel, new GUILayoutOption[0]);
            this.extra = EditorGUILayout.TextArea(this.extra, FsmEditorStyles.TextAreaWithWordWrap, new GUILayoutOption[] { GUILayout.ExpandHeight(true) });
            this.area = (ScoutArea) EditorGUILayout.EnumPopup("Where does it happen", this.area, new GUILayoutOption[0]);
            this.frequencyIndex = EditorGUILayout.Popup("How often does it happen", this.frequencyIndex, this.frequencyChoices, new GUILayoutOption[0]);
            this.email = EditorGUILayout.TextField(new GUIContent("Your E-mail", "Optional, but recommended if you want updates on your bug report."), this.email, new GUILayoutOption[0]);
            EditorGUILayout.EndScrollView();
            GUILayout.Space(10f);
            FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            if (GUILayout.Button("Submit Report", new GUILayoutOption[0]))
            {
                if (!this.isValid)
                {
                    EditorUtility.DisplayDialog("Submit Bug Report", this.errorString, "OK");
                }
                else
                {
                    this.SubmitBugReport();
                }
                GUIUtility.ExitGUI();
            }
            else
            {
                GUI.enabled = true;
                if (GUILayout.Button(new GUIContent("Copy", "Copy report to clipboard."), new GUILayoutOption[] { GUILayout.MaxWidth(100f) }))
                {
                    this.CopyReportToClipboard();
                }
                if (GUILayout.Button(new GUIContent("Reset"), new GUILayoutOption[] { GUILayout.MaxWidth(100f) }))
                {
                    GUIUtility.keyboardControl = 0;
                    this.Reset();
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(10f);
                if (GUI.changed)
                {
                    this.UpdateGUI();
                    GUIUtility.ExitGUI();
                }
            }
        }

        private void Reset()
        {
            string newLine = Environment.NewLine;
            this.area = ScoutArea.Editor;
            this.frequencyIndex = 0;
            this.description = "";
            this.extra = "1) What happened" + newLine + newLine + newLine + newLine + "2) How can we reproduce it" + newLine + newLine;
            this.email = EditorPrefs.GetString("PlayMaker.UserEmail", "");
            base.Repaint();
        }

        private void SubmitBugReport()
        {
            WWWForm form = new WWWForm();
            form.AddField("Description", this.description);
            form.AddField("ScoutProject", "playMaker");
            form.AddField("ScoutUsername", "Test User");
            form.AddField("ScoutArea", this.area.ToString());
            form.AddField("Extra", this.BuildReportBody(false));
            if (!string.IsNullOrEmpty(this.email))
            {
                form.AddField("Email", this.email);
                EditorPrefs.SetString("PlayMaker.UserEmail", this.email);
            }
            this.www = new WWW("https://hutonggames.fogbugz.com/ScoutSubmit.asp", form);
        }

        private void Update()
        {
            if (this.www != null)
            {
                if (this.www.isDone)
                {
                    EditorUtility.ClearProgressBar();
                    EditorUtility.DisplayDialog("PlayMaker", string.IsNullOrEmpty(this.www.error) ? "Bug Report Received.\nThanks!" : ("Error Submitting Report:\n" + this.www.error), "OK");
                    this.www = null;
                }
                else
                {
                    EditorUtility.DisplayProgressBar("PlayMaker", "Submitting bug report...", this.www.uploadProgress);
                }
            }
        }

        private void UpdateGUI()
        {
            this.isValid = this.IsValidSetup();
        }

        private enum ScoutArea
        {
            Editor,
            Runtime,
            Actions,
            API,
            Documentation
        }
    }
}

